package com.mt.customer.service.model;

import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;
import com.mt.customer.entity.ContractEntity;
import com.mt.customer.mapper.ContractEntityMapper;

/**
 * 电子合同
 */
@Component
public class ContractServiceModel {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ContractServiceModel.class);

    @Autowired
    private ContractEntityMapper contractEntityMapper;

    public ServiceResult<List<ContractEntity>> selectList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        Assert.notNull(this.contractEntityMapper, "Property 'contractEntityMapper' is required.");
        ServiceResult<List<ContractEntity>> result = new ServiceResult<List<ContractEntity>>();
        try {
            if (pagerInfo != null) {
                PageHelper.startPage(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
            }
            result.setResult(this.contractEntityMapper.selectList(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ContractServiceModel][selectList]：query selectList occur exception", e);
        }
        return result;
    }

    public ServiceResult<ContractEntity> selectInfo(Map<String, Object> params) {
        Assert.notNull(this.contractEntityMapper, "Property 'contractEntityMapper' is required.");
        ServiceResult<ContractEntity> result = new ServiceResult<ContractEntity>();
        try {
            result.setResult(this.contractEntityMapper.selectByPrimaryKey(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ContractServiceModel][selectInfo]：query selectInfo occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> insert(ContractEntity contractEntity) {
        Assert.notNull(this.contractEntityMapper, "Property 'contractEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.contractEntityMapper.insert(contractEntity);
            if (save > 0) {
                id = contractEntity.getContractId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ContractServiceModel][insert]：query insert occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateRelate(ContractEntity contractEntity) {
        Assert.notNull(this.contractEntityMapper, "Property 'contractEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.contractEntityMapper.updateBySelective(contractEntity);
            if (save > 0) {
                id = contractEntity.getContractId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ContractServiceModel][updateBySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> deleteByPrimaryKey(Integer contractId) {
        Assert.notNull(this.contractEntityMapper, "Property 'contractEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            id = this.contractEntityMapper.deleteByPrimaryKey(contractId);
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ContractServiceModel][deleteByPrimaryKey]:query deleteByPrimaryKey occur exception", e);
        }
        return result;
    }
}
